package dl.transactionscript.service;

import java.sql.ResultSet;
import java.sql.SQLException;

import dl.common.service.iface.ContractServiceInterface;
import dl.exception.handler.ApplicationException;
import dl.martinfowler.wrapper.MfDate;
import dl.martinfowler.wrapper.Money;
import dl.transactionscript.gateway.TableDataGateway;

public class ContractService implements ContractServiceInterface {

	private TableDataGateway tableDataGateway;

	public ContractService() {
		tableDataGateway = new TableDataGateway();
	}

	public long insertContractInformation(long productNumber, Money revenue,
			MfDate dateSigned) throws ApplicationException {
		long newContractId = 0;
		try {
			newContractId = tableDataGateway.insertContract(productNumber,
					revenue, dateSigned);
		} catch (SQLException e) {
			throw new ApplicationException(e);
		}
		return newContractId;
	}

	public void deleteContractInformation(long id) throws ApplicationException {
		try {
			tableDataGateway.deleteContract(id);
		} catch (SQLException e) {
			throw new ApplicationException(e);
		}
	}

	public void showContractList() throws ApplicationException {
		try {
			ResultSet rs = tableDataGateway.findProductContractAll();
			System.out.println("Product + Contarct : ");
			while (rs.next()) {
				System.out.println(rs.getInt(1) + ":" + rs.getString(2) + ":"
						+ rs.getString(3) + ":" + rs.getString(6) + ":"
						+ rs.getString(7));
			}
		} catch (SQLException e) {
			throw new ApplicationException(e);
		}
	}
}
